package cn.felord.debezium.utils;

import java.math.BigInteger;

public class String2AcsII16 {
    //28095    port
    public static int hex2Int(String hexString){
        int i10 = Integer.parseInt(hexString, 16);
        return i10;
    }
    public static String hex2Int(String hexString,int lenth){
        int i10 = Integer.parseInt(hexString, 16);
        if(i10 < 10){
            return "0"+ i10;
        }
        return i10 + "";
    }
    public static String int10to16_4(int asciiValue10){
        String s16 = Integer.toHexString(asciiValue10);
        int cut = 4 - s16.length();
        if(s16.length() < 4){
            for(int j = 0;j < cut ;j++){
                s16 = "0" + s16;
            }
        }
//        System.out.println( " 四位的 16进制 值为：" + s16);
        return s16;
    }
    public static String int10to16(int asciiValue10,int length){
        String s16 = Integer.toHexString(asciiValue10);
        int cut = length - s16.length();
        if(s16.length() < length){
            for(int j = 0;j < cut ;j++){
                s16 = "0" + s16;
            }
        }
        return s16;
    }
    public static String int10to16_2(int asciiValue10){
        String s16 = Integer.toHexString(asciiValue10);
        int cut = 2 - s16.length();
        if(s16.length() < 2){
            for(int j = 0;j < cut ;j++){
                s16 = "0" + s16;
            }
        }
//        System.out.println( " 二位的 16进制 值为：" + s16);
        return s16;
    }
    public static String hexToString(String hex) {
        return new String(new BigInteger(hex, 16).toByteArray());
    }
    public static int string2Char2asciiValue(String str){
        char c = str.charAt(0);
        int asciiValue = c;  //char 转 int
        return asciiValue;
    }
    /**
     * 字符串转 16 两位（两字节）进制的字符串 占位00
     * @param str
     * @return
     */
    public static String string2hex(String str){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int asciiValue = c;  //char 转 int
            String s = int10to16_2(asciiValue);
            sb.append(s);
        }
        return sb.toString();
    }
    public static String string2hex(String str,int len){
        String s16 = string2hex(str);
        if (s16.length() < 16) {
            int cut = len - s16.length();
            for (int i = 0; i < cut; i++) {
                s16 = "0" + s16;
            }
        }
        return s16;
    }
    public static String string2hex_UP(String str,int len){
        String s16 = string2hex(str);
        if (s16.length() < 16) {
            int cut = len - s16.length();
            for (int i = 0; i < cut; i++) {
                s16 = "0" + s16;
            }
        }
        return s16.toUpperCase();
    }
    public static String string2Char2asciiValueint10to16(String str){
        return int10to16_4(string2Char2asciiValue(str));
    }
    public static String string2Char2asciiValueint10to16_2(String str){
        return int10to16_2(string2Char2asciiValue(str));
    }
    public static void main(String[] args) {
        System.out.println(hexToString("fe42ff40fec0422cbfff"));
        System.out.println(hexToString("4B44"));
        System.out.println(hexToString("4B44544330303033000000000000004100000000000000420000000000000043000000000000004400000000000000450000000000000000"));
    }

}
